package com.bjdd.jdy.ui.exam;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.ExamBean;
import com.bjdd.jdy.entity.ExamConfig;
import com.bjdd.jdy.entity.ExamResult;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.bjdd.jdy.view.other.QuestionLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.widget.LinearLayout.VERTICAL;

public class ExamActivity extends BaseActivity<ExamPresenter, ExamModel> implements ExamContract.View {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.ll_exam_question_box)
    LinearLayout llExamQuestionBox;
    @Bind(R.id.btn_exam_commit)
    Button commit;
    private int clickType = 0;//0 提交数据 1重新考试
    private List<QuestionLinearLayout> questionLinearLayouts;
    private ExamConfig examConfig;
    @Override
    public int getLayoutId() {
        return R.layout.activity_exam;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("闪电加入");
        presenter.getExam(SpUtil.getToken(), SpUtil.getUser().getCarrierId());
    }

    @Override
    public void passExam() {
        finish();
    }

    @Override
    public void setView(ExamResult result) {
        clickType = 0;
        //llExamQuestionBox.removeAllViews();
        questionLinearLayouts = new ArrayList<>();
        examConfig = result.getConfig();
        List<ExamBean> examBeens = result.getQuestion();
        for (int i=0;i<examBeens.size();i++){
            QuestionLinearLayout ll = new QuestionLinearLayout(context);
            ll.setExamBean(examBeens.get(i));
            questionLinearLayouts.add(ll);
            llExamQuestionBox.addView(ll, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
    }

    @OnClick({R.id.toolbar_back, R.id.btn_exam_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.btn_exam_commit:
                if (clickType == 1){
                    presenter.getExam(SpUtil.getToken(),SpUtil.getUser().getCarrierId());
                }else if (clickType == 0){
                    List<QuestionLinearLayout> question = new ArrayList<>();//错误题目集合
                    int count = 0;//答对题目数量
                    for (int i=0;i<questionLinearLayouts.size();i++){
                        L.d("是否回答："+questionLinearLayouts.get(i).isHasAnswer()+"----是否正确："+questionLinearLayouts.get(i).isResult());
                        if (!questionLinearLayouts.get(i).isHasAnswer()){
                            T.show("还有题目没有回答哦~");
                            return;
                        }
                        if (questionLinearLayouts.get(i).isResult()){
                            count ++;
                        }else {
                            question.add(questionLinearLayouts.get(i));
                        }
                    }
                    double score = count * examConfig.getPointScore();
                    if (score < examConfig.getPassScore()){
                        T.show("考试没有通过");
                        llExamQuestionBox.removeAllViews();
                        for (int i=0;i<question.size();i++){
                            llExamQuestionBox.addView(question.get(i));
                        }
                        clickType = 1;
                        commit.setText("再来一次");
                    }else {
                        presenter.commitExam(SpUtil.getToken(),4,SpUtil.getUser().getCarrierId());
                    }
                }

                break;
        }
    }

}
